// 封装本地存储的操作
const TOKEN_NAME = 'token'
const CHANNEL_NAME = 'channelList'
const SEARCH_HISTORY_NAME = 'historyList'
export const setStorageToken = (payload) => {
  localStorage.setItem(TOKEN_NAME, JSON.stringify(payload))
}
export const getStorageToken = () =>
  // 获取token的结果，还需要将其返回出去
  JSON.parse(localStorage.getItem(TOKEN_NAME)) || {}

// 退出登录的时候，需要使用该函数实现把本地存储的token清除掉
export const removeStorageToken = () => {
  localStorage.removeItem(TOKEN_NAME)
}

export const setStorageChannel = (payload) => {
  localStorage.setItem(CHANNEL_NAME, JSON.stringify(payload))
}

export const getStorageChannel = () =>
  // 获取channel的结果，还需要将其返回出去
  // 不需要去设置默认值 空对象，因为一旦本地中获取不到频道的数据，就会去发送请求来获取频道的数据
  JSON.parse(localStorage.getItem(CHANNEL_NAME))

// 存搜索的历史记录
export const setStorageSearchHistory = (payload) => {
  localStorage.setItem(SEARCH_HISTORY_NAME, JSON.stringify(payload))
}
export const getStorageSearchHistory = () =>
  JSON.parse(localStorage.getItem(SEARCH_HISTORY_NAME)) || []
